package com.ossjk.qlh.order.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.core.util.CommonUtils;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.order.entity.OrderTc;
import com.ossjk.qlh.order.entity.Orders;
import com.ossjk.qlh.order.entity.Otrs;
import com.ossjk.qlh.order.service.IOrderitmService;
import com.ossjk.qlh.order.service.IOrdersService;
import com.ossjk.qlh.order.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.*;

import static java.util.Arrays.asList;

/**
 * 订单
 *
 * @author liuxi
 * @since 2020-08-14
 */
@Api(tags = "订单")
@RestController
@RequestMapping("/order/currentorder")
public class CurrentOrdersController extends BaseController {

    @Autowired
    private IOrdersService iOrdersService;

    @Autowired
    private IOrderitmService iOrderitmService;

    @ApiOperation(value = "当前订单管理列表")
    @RequiresPermissions("00200020")
    @PostMapping("/orderlist")
    public ResponseBean<Page<CurrentOrderVo>> orderlist(@RequestBody RequestBean requestBean) {
        QueryWrapper<CurrentOrderVo> queryWrapper = new QueryWrapper<CurrentOrderVo>();
        Integer i = 1;
        if (!CommonUtil.isBlank(requestBean.getParam().get("type"))) {
            i = Integer.parseInt(requestBean.getParam().get("type").toString());
        }
        System.out.println(requestBean.getParam().get("type"));
        Page<CurrentOrderVo> page = iOrdersService.selectLunchOrderVo(requestBean.getPage(), i, CommonUtil.formatDate(new Date(), "yyyy-MM-dd"));
        List<CurrentOrderVo> records = page.getRecords();

        records.forEach(item -> {
            QueryWrapper<OrderTc> queryWrapper2 = new QueryWrapper<OrderTc>();
            queryWrapper2.eq("o.state", 1);
            queryWrapper2.eq("o.type", requestBean.getParam().get("type"));
            queryWrapper2.eq("o.eid ", item.getEid());
            System.out.println(iOrdersService.selectTcByEid(queryWrapper2));
            item.setExptxt(iOrdersService.selectTcByEid(queryWrapper2));
        });
        page.setRecords(records);
        return responseSuccess(page);
    }

    @ApiOperation(value = "接单打印-新订单-未接单")
    @PostMapping("/selectJdPrintOne")
    public ResponseBean<Page<JdOrderVo>> selectJdPrintOne(@RequestBody RequestBean requestBean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        //企业类型，一个订单只能选一个套餐
        //动态拼接条件
        if (!CommonUtil.isBlank(requestBean.getParam().get("otype"))) {
            queryWrapper.eq("o.type", requestBean.getParam().get("otype"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("ename"))) {
            queryWrapper.eq("en.ename", requestBean.getParam().get("ename"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("ostate")) && Integer.parseInt(requestBean.getParam().get("ostate").toString()) != 3) {
            queryWrapper.eq("o.state", requestBean.getParam().get("ostate"));
        }
        return responseSuccess(iOrdersService.selectJdPrintOne(queryWrapper));
    }

    @ApiOperation(value = "接单打印-新订单-未接单")
    @PostMapping("/selectJdPrint")
    public ResponseBean<Page<JdOrderVo>> selectJdPrint(@RequestBody RequestBean requestBean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        //企业类型，一个订单只能选一个套餐
        //动态拼接条件
        if (!CommonUtil.isBlank(requestBean.getParam().get("otype"))) {
            queryWrapper.eq("o.type", requestBean.getParam().get("otype"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("ename"))) {
            queryWrapper.eq("en.ename", requestBean.getParam().get("ename"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("ostate")) && Integer.parseInt(requestBean.getParam().get("ostate").toString()) != 3) {
            queryWrapper.eq("o.state", requestBean.getParam().get("ostate"));
        }


        //查询出接单打印所有信息，并且遍历得到对应的套餐列表
        List<JdOrderVo> list = iOrdersService.selectJdPrint(queryWrapper);
        list.forEach(item -> {
            QueryWrapper queryWrapper2 = new QueryWrapper();
            //判断是否传递了企业类型，不为空则为虚拟用户，执行合并多条套餐逻辑
            if (!CommonUtil.isBlank((String) requestBean.getParam().get("etype"))) {
                queryWrapper2.eq("en.type", 2);
            } else {
                queryWrapper.eq("en.type", 1);
            }
            queryWrapper2.eq("oid", item.getOid());
            List<TcList> selectTcList = iOrdersService.selectTcList(queryWrapper2);
            item.setSelectTc(selectTcList);
        });
        System.out.println(list);
        return responseSuccess(list);
    }


    @ApiOperation(value = "修改订单状态")
    @PostMapping("/update")
    public ResponseBean<Page<Orders>> update(@RequestBody RequestBean requestBean) {
        List list = new ArrayList();
        list = (List) requestBean.getParam().get("ids");
        System.out.println(list);
        Collection collection = new ArrayList();
        list.forEach(item -> {
            Orders orders = new Orders();
            orders.setId((String) item);
            orders.setState(2);
            orders.setJdtm(new Date());
            collection.add(orders);
        });
        return responseSuccess(iOrdersService.updateBatchById(collection));
    }

    @ApiOperation(value = "修改配送状态")
    @PostMapping("/updateps")
    public ResponseBean<Page<Orders>> updateps(@RequestBody RequestBean requestBean) {
        List list = new ArrayList();
        list = (List) requestBean.getParam().get("ids");
        System.out.println(list);
        Collection collection = new ArrayList();
        list.forEach(item -> {
            Orders orders = new Orders();
            orders.setId((String) item);
            orders.setState(3);
            orders.setSendtm(new Date());
            collection.add(orders);
        });
        return responseSuccess(iOrdersService.updateBatchById(collection));
    }


    //	======================================================================
    @ApiOperation(value = "统计菜品数")
    @PostMapping("/countTc")
    public ResponseBean<List> tongji(@RequestBody RequestBean requestBean) {

        Map<Object, Integer> map = new HashMap<Object, Integer>();
        List list = new ArrayList();
        List listData = new ArrayList();
        List<countFc> countFc = countFc(requestBean);
        List<countQc> countQc = countQc(requestBean);
        List<countZc> countZ = countZc(requestBean);
        List<countZs> countZs = countZs(requestBean);
        List<countQt> countQt = countQt(requestBean);
        listData.addAll(countFc);
        listData.addAll(countQc);
        listData.addAll(countZ);
        listData.addAll(countZs);
        for (int i = 0; i < countQt.size(); i++) {
            List list1 = (List) JSON.parse(countQt.get(i).getOtrs());
            for (int j = 0; j < list1.size(); j++) {
                list.add(JSON.parseObject(JSON.toJSONString(list1.get(j)), Otrs.class).getName());
            }
        }

        for (Object str : list) {
            Integer i = 1; //定义一个计数器，用来记录重复数据的个数
            if (map.get(str) != null) {
                i = map.get(str) + 1;
            }
            map.put(str, i);
        }
        System.out.println(map.toString());
        list.clear();
        Iterator<Object> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String s = iterator.next().toString();
            countQtVo countQt1 = new countQtVo();
            countQt1.setOtrs(map.get(s));
            countQt1.setTitle(s);
            list.add(countQt1);
        }
        listData.addAll(list);
        return responseSuccess(listData);
    }


    /**
     * 主菜个数
     *
     * @param requestBean
     * @return
     */
    public List<countZc> countZc(@RequestBody RequestBean requestBean) {
        QueryWrapper<countZc> queryWrapperZc = new QueryWrapper<countZc>();

        queryWrapperZc.eq("o.type", 1);
//        queryWrapperZc.eq("o.state", 1);
        if (!CommonUtil.isBlank(requestBean.getParam().get("state"))) {
            queryWrapperZc.like("o.state", requestBean.getParam().get("state"));
        }

        if (!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapperZc.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapperZc.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countZC(queryWrapperZc);
    }


    /**
     * 副菜
     *
     * @param requestBean
     * @return
     */
    public List<countFc> countFc(@RequestBody RequestBean requestBean) {
        QueryWrapper<countFc> queryWrapper = new QueryWrapper<countFc>();
        queryWrapper.eq("o.type", 1);
//        queryWrapper.eq("o.state", 1);
        if (!CommonUtil.isBlank(requestBean.getParam().get("state"))) {
            queryWrapper.like("o.state", requestBean.getParam().get("state"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countFC(queryWrapper);
    }


    /**
     * 青菜
     *
     * @param requestBean
     * @return
     */
    public List<countQc> countQc(@RequestBody RequestBean requestBean) {
        QueryWrapper<countQc> queryWrapper = new QueryWrapper<countQc>();
        queryWrapper.eq("o.type", 1);
//        queryWrapper.eq("o.state", 1);
        if (!CommonUtil.isBlank(requestBean.getParam().get("state"))) {
            queryWrapper.like("o.state", requestBean.getParam().get("state"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countQC(queryWrapper);
    }


    /**
     * 主食
     *
     * @param requestBean
     * @return
     */
    public List<countZs> countZs(@RequestBody RequestBean requestBean) {
        QueryWrapper<countZs> queryWrapper = new QueryWrapper<countZs>();
        queryWrapper.eq("o.type", 1);
//        queryWrapper.eq("o.state", 1);
        if (!CommonUtil.isBlank(requestBean.getParam().get("state"))) {
            queryWrapper.like("o.state", requestBean.getParam().get("state"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countZs(queryWrapper);
    }

    /**
     * 其它
     *
     * @param requestBean
     * @return
     */
    public List<countQt> countQt(@RequestBody RequestBean requestBean) {
        QueryWrapper<countQt> queryWrapper = new QueryWrapper<countQt>();
        queryWrapper.eq("o.type", 1);
//        queryWrapper.eq("o.state", 1);
        if (!CommonUtil.isBlank(requestBean.getParam().get("state"))) {
            queryWrapper.like("o.state", requestBean.getParam().get("state"));
        }

        if (!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countQt(queryWrapper);
    }
}
